package com.barry.generator

import com.barry.bean.{ResultModel, SysCodeEnum}
import com.barry.dto._
import com.barry.utils.Debug
import org.slf4j.LoggerFactory

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.util.control.NonFatal
import scala.util.{Failure, Success}

/**
  * @ClassName ReportAction
  * @Description 报表生成流程
  * @Author wangxuexing
  * @Date 2020/2/20 10:40
  * @Version 1.0
  */
trait ReportAction {
  private val logger = LoggerFactory.getLogger(getClass)
  /**
    * 报表导出流程
    * @return
    */
  def execute: ResultModel = {
    var result:ResultModel = null
    var count = Counter.count
    logger.info(s"---1生成报表前运行个数:${count}----------")
    if(count >= Counter.MAX_COUNT.intValue()){
      result = new ResultModel(SysCodeEnum.CURRENT_REPORT_LIMIT)
    } else {
      Counter.increment
      count = Counter.count
      logger.info(s"---2启动生成报表运行个数:${count}----------")
      var reportId: Long = -1
      try {
        //1. 前置检查
        preCheck

        //2. 开始生成报表纪录
        val reportConfig = setReportConfig
        reportId = startGenerate

        //3. 生成报表处理
//        val output = Future {
          //添加当前子线程到线程管理池
          ReportThreadManager.put(reportId, Thread.currentThread())
          //生成报表
          val output = Debug.trace(s"${reportConfig.reportType.toString}-generateReport:"){
            generateReport(reportId, reportConfig)
          }
          //将此子线程从线程管理池移除
          ReportThreadManager.remove(reportId)
          output
//        }
       // Thread.sleep(12000)
        /*output.onComplete {
          case Success(v) => {
            successGenerate(ExportReportSuccessInput(reportId, v))
            Counter.decrement
            count = Counter.count
            logger.info(s"---3结束报表生成运行个数:${count}----------")
          }
          case Failure(e) => {
            failureGenerate(ExportReportFailureInput(reportId, e))
            Counter.decrement
            count = Counter.count
            logger.info(s"---3结束报表生成运行个数:${count}----------")
          }
        }*/

        //4. 后置检查
        postCheck

        result = new ResultModel(SysCodeEnum.EXPORT_SUCCESS)
      } catch {
        case NonFatal(e) =>
          Counter.decrement
          count = Counter.count
          logger.info(s"---3结束报表生成运行个数:${count}----------")
          failureGenerate(ExportReportFailureInput(reportId, e))
          throw e
      } finally {
        // 无操作
      }
    }
    result
  }

  /**
    * 前置条件检查：动作、状态等业务逻辑
    */
  def preCheck


  /**
    * 设置报表属性
    * @return
    */
  def setReportConfig: ReportConfig

  /**
    * 开始生成报表纪录
    */
  def startGenerate(): Long = {
    1L
  }

  /**
    * 下载文件名
    * @return
    */
  def downloadFileName: String = setReportConfig.reportType.toString

  /**
    * 根据数据生成报表
    * @return
    */
  @throws(classOf[Exception])
  def generateReport(reportId: Long, reportConfig: ReportConfig) : ReportOutput

  /**
    * 成功生成报表纪录
    * @param result: ExportReportSuccessInput
    */
  def successGenerate(result: ExportReportSuccessInput): Unit = {
//    new ExportReportRecordSuccessAction(result).execute
    //TODO
  }

  /**
    * 失败生成报表纪录
    * @param result: ExportReportFailureInput
    */
  def failureGenerate(result: ExportReportFailureInput): Unit = {
//    new ExportReportRecordFailureAction(result).execute
    //TODO
  }

  /**
    * 后置检查
    */
  def postCheck = {
    // 无后置检查
  }
}